/**********************************************************************************************/
/* FBL_DatabaseManager.cpp                                                 					  */
/*                                                                       					  */
/* Copyright Paradigma, 1993-2006															  */
/* All Rights Reserved                                                   					  */
/**********************************************************************************************/

#include <VShared/FBL/publ/Headers/StdAfx.h>

#include <VShared/FBL/publ/Util_classes/FBL_DatabaseManager.h>
#include <VShared/FBL/publ/Util_classes/FBL_String.h>
#include <VShared/FBL/publ/Util_Strings/FBL_ustdio.h>


/**********************************************************************************************/
FBL_Begin_Namespace


/**********************************************************************************************/
DatabaseManager*	DatabaseManager::mInstance = 0;


/**********************************************************************************************/
DatabaseManager* DatabaseManager::get_DatabaseManager( void ) throw()
{
	if( mInstance == NULL )
	{
	#if DEBUG_NEW == 2 
		StWatchLeakStopper stopper;
	#endif // DEBUG_NEW
	
		mInstance = new DatabaseManager;
	}
		
	return 	mInstance;	
}

/**********************************************************************************************/
void DatabaseManager::Destroy_Databases( bool inIsRemoteOnly ) throw()
{
	ulong Count = mDatabases->get_Count();
	
	for( ulong i = 1; i <= Count ; ++i )
	{
		I_Database* pdb = mDatabases->get_ItemAt( i );
		
		try
		{
			// Close and UnRegister some databases - depends on inIsRemoteOnly.
			bool isRemote = pdb->get_IsRemote();

			if( (isRemote && inIsRemoteOnly) ||
				(isRemote == false && inIsRemoteOnly == false ) )
			{	
				if( pdb->get_IsOpen() )
					pdb->Close();
			}
		}
		catch(...)
		{
		}
			
		UnRegisterDatabase( pdb );
	} 

	// We could not throw away this array.
	// It will die along with DatabaseManager instance.
	//mDatabases = NULL;
}


/**********************************************************************************************/
DatabaseManager::DatabaseManager()
{
	mDatabases = new ArrayOfDatabases( kNotOwnItems );
}


/**********************************************************************************************/
void DatabaseManager::RegisterDatabase( I_Database* inDB ) throw()
{
	mDatabases->AddItem( inDB );
}


/**********************************************************************************************/
void DatabaseManager::UnRegisterDatabase( I_Database* inDB ) throw()
{
	mDatabases->RemoveItem( inDB );
}


/**********************************************************************************************/
ulong DatabaseManager::get_DatabaseCount( void ) throw()
{
	return mDatabases->get_Count();
}


/**********************************************************************************************/
ulong DatabaseManager::FindIndex( I_Database* inDB ) throw()
{
	return mDatabases->FindIndex( inDB );
}


/**********************************************************************************************/
I_Database* DatabaseManager::get_Database( ulong inIndex ) throw()
{
	if( inIndex > mDatabases->get_Count() )
		return NULL;
	else
		return mDatabases->get_ItemAt( inIndex );
}


/**********************************************************************************************/
I_Database* DatabaseManager::get_Database( const UChar* inName ) throw()
{
	ulong Count = mDatabases->get_Count();
	for( ulong i = 1; i <= Count ; ++i )
	{
		I_Database* pdb = mDatabases->get_ItemAt( i );

		String dbName = pdb->get_Name();
		dbName.remove( dbName.find('.') );

		if( dbName.caseCompare( inName, 0 ) == 0 )
			return pdb;

/*
		if( vu_stricmp( inName, pdb->get_Name().c_str() ) == 0 )
			return pdb;
*/
	} 
	
	return NULL;
}


/**********************************************************************************************/
I_Database* DatabaseManager::get_MasterDb( void ) throw()
{
	String masterDBName("master.vdb");

	ulong Count = mDatabases->get_Count();
	for( ulong i = 1; i <= Count ; ++i )
	{
		I_Database* pdb = mDatabases->get_ItemAt( i );
		if( masterDBName == pdb->get_Name() )
			return pdb;
	} 
	
	return NULL;
}


/**********************************************************************************************/
FBL_End_Namespace
